package com.wodsy.phoenix.dataservice.shopc.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.wodsy.phoenix.datadao.shopc.dao.GoodsClassInShopCascadeDao;
import com.wodsy.phoenix.datadao.shopc.dao.GoodsClassInShopDao;
import com.wodsy.phoenix.datadao.shopc.dao.GoodsGcisRelationDao;
import com.wodsy.phoenix.dataobject.base.Pager;
import com.wodsy.phoenix.dataobject.base.PhoenixGenericResponse;
import com.wodsy.phoenix.dataobject.base.PhoenixResponseWrapper;
import com.wodsy.phoenix.dataobject.base.ResponseCodeConstants;
import com.wodsy.phoenix.dataobject.shopc.model.GoodsClassInShop;
import com.wodsy.phoenix.dataobject.shopc.model.GoodsClassInShopCascade;
import com.wodsy.phoenix.dataobject.shopc.model.GoodsGcisRelation;
import com.wodsy.phoenix.dataservice.shopc.service.GoodsClassInShopService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Service(interfaceName="com.wodsy.phoenix.dataservice.shopc.service.GoodsClassInShopService" )
public class GoodsClassInShopServiceImpl implements GoodsClassInShopService {

	protected Logger logger = Logger.getLogger(this.getClass());
	
	@Autowired
	private GoodsClassInShopDao goodsClassInShopDao;
	@Autowired
	private GoodsClassInShopCascadeDao goodsClassInShopCascadeDao;
	@Autowired
	private GoodsGcisRelationDao goodsGcisRelationDao;


	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<PhoenixGenericResponse> addGoodsClassInShop(GoodsClassInShop gc) {
		try {
			gc.setCreateTime(new Date());
			goodsClassInShopDao.save(gc);
			return new PhoenixResponseWrapper<PhoenixGenericResponse>(new PhoenixGenericResponse(gc.getId()));
		} catch (Exception e) {
			logger.error("",e);
			return new PhoenixResponseWrapper<PhoenixGenericResponse>(ResponseCodeConstants.EXCEPTION,"exception occur");
		}
	}

	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<PhoenixGenericResponse> addGoodsClassInShops(List<GoodsClassInShop> gcs) {
		try {
			for(GoodsClassInShop gc : gcs){
                addGoodsClassInShop(gc);
            }
			return new PhoenixResponseWrapper<PhoenixGenericResponse>();
		} catch (Exception e) {
			logger.error("", e);
			return new PhoenixResponseWrapper<PhoenixGenericResponse>(ResponseCodeConstants.EXCEPTION,"exception occur");
		}
	}

	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<PhoenixGenericResponse> delGoodsClassInShop(Integer gcId) {
		try {
			GoodsClassInShopCascade gc = goodsClassInShopCascadeDao.get(gcId);
			if(gc != null){
                goodsClassInShopCascadeDao.delete(gc);
                List<GoodsClassInShopCascade> list = goodsClassInShopCascadeDao.findAllByProperty( "pid", gcId);
                for (GoodsClassInShopCascade gcc : list) {
                    goodsClassInShopCascadeDao.delete(gcc);
                }
            }
			return new PhoenixResponseWrapper<PhoenixGenericResponse>();
		} catch (Exception e) {
			logger.error("", e);
			return new PhoenixResponseWrapper<PhoenixGenericResponse>(ResponseCodeConstants.EXCEPTION,"exception occur");
		}
	}

	public static void main(String[] args) {
		com.alibaba.dubbo.container.Main.main(args);
	}

	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<PhoenixGenericResponse> updateGoodsClassInShop(GoodsClassInShop gc) {
		try {
			gc.setUpdateTime(new Date());
			goodsClassInShopDao.update(gc);
			return new PhoenixResponseWrapper<PhoenixGenericResponse>(new PhoenixGenericResponse(gc.getId()));
		} catch (Exception e) {
			logger.error("", e);
			return new PhoenixResponseWrapper<PhoenixGenericResponse>(ResponseCodeConstants.EXCEPTION,"exception occur");
		}
	}

	@Override
	@Transactional(value="xa",readOnly=true)
	public PhoenixResponseWrapper<GoodsClassInShop> findGoodsClassInShopsBy(Integer shopId) {
		try {
			Pager<GoodsClassInShop> pager = new Pager<GoodsClassInShop>();
			List<GoodsClassInShop> list = goodsClassInShopDao.findAllByProperty("shopId", shopId);
			if (list != null && !list.isEmpty()){
                pager.setItems(list);
                pager.setTotalCount(list.size());
                return new PhoenixResponseWrapper<GoodsClassInShop>(pager);
            }else{
                return new PhoenixResponseWrapper<GoodsClassInShop>(ResponseCodeConstants.EMPTY, "empty result");
            }
		} catch (Exception e) {
			logger.error("", e);
			return new PhoenixResponseWrapper<GoodsClassInShop>(ResponseCodeConstants.EXCEPTION,"exception occur");
		}
	}

	@Override
	@Transactional(value="xa",readOnly=true)
	public PhoenixResponseWrapper<GoodsClassInShopCascade> findGoodsClassInShopCascadesBy(
			Integer shopId) {
		try {
			Pager<GoodsClassInShopCascade> pager = new Pager<GoodsClassInShopCascade>();
			List<GoodsClassInShopCascade> list =  goodsClassInShopCascadeDao.findAllByProperty("shopId", shopId);
			if (list != null && !list.isEmpty()){
                pager.setItems(list);
                pager.setTotalCount(list.size());
                return new PhoenixResponseWrapper<GoodsClassInShopCascade>(pager);
            }else{
                return new PhoenixResponseWrapper<GoodsClassInShopCascade>(ResponseCodeConstants.EMPTY, "empty result");
            }
		} catch (Exception e) {
			logger.error("", e);
			return new PhoenixResponseWrapper<GoodsClassInShopCascade>(ResponseCodeConstants.EXCEPTION,"exception occur");
		}
	}

	@Override
	@Transactional(value="xa",readOnly=true)
	public PhoenixResponseWrapper<GoodsGcisRelation> findGoodsGcisRelations(Integer gcId) {
		try {
			Pager<GoodsGcisRelation> pager = new Pager<GoodsGcisRelation>();
			GoodsClassInShopCascade gc = goodsClassInShopCascadeDao.get(gcId);
			if(gc != null && gc.getGoodsRelations() != null && !gc.getGoodsRelations().isEmpty()){
                Set<GoodsGcisRelation> set = gc.getGoodsRelations();
				List<GoodsGcisRelation> list = new ArrayList<GoodsGcisRelation>(set);
				pager.setItems(list);
				pager.setTotalCount(list.size());
				return new PhoenixResponseWrapper<GoodsGcisRelation>(pager);
            }else{
                return new PhoenixResponseWrapper<GoodsGcisRelation>(ResponseCodeConstants.EMPTY, "empty result");
            }
		} catch (Exception e) {
			logger.error("", e);
			return new PhoenixResponseWrapper<GoodsGcisRelation>(ResponseCodeConstants.EXCEPTION,"exception occur");
		}
	}

	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<PhoenixGenericResponse> addGoodsGcisRelation(GoodsGcisRelation rel) {
		try {
			goodsGcisRelationDao.save(rel);
			return new PhoenixResponseWrapper<PhoenixGenericResponse>(new PhoenixGenericResponse(rel.getId()));
		} catch (Exception e) {
			logger.error("", e);
			return new PhoenixResponseWrapper<PhoenixGenericResponse>(ResponseCodeConstants.EXCEPTION,"exception occur");
		}
	}

	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<PhoenixGenericResponse> addGoodsGcisRelations(List<GoodsGcisRelation> rels) {
		try {
			for(GoodsGcisRelation rel : rels){
                addGoodsGcisRelation(rel);
            }
			return new PhoenixResponseWrapper<PhoenixGenericResponse>();
		} catch (Exception e) {
			logger.error("", e);
			return new PhoenixResponseWrapper<PhoenixGenericResponse>(ResponseCodeConstants.EXCEPTION,"exception occur");
		}
	}

	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<PhoenixGenericResponse> delGoodsGcisRelation(GoodsGcisRelation rel) {
		try {
			goodsGcisRelationDao.delete(rel);
			return new PhoenixResponseWrapper<PhoenixGenericResponse>();
		} catch (Exception e) {
			logger.error("", e);
			return new PhoenixResponseWrapper<PhoenixGenericResponse>(ResponseCodeConstants.EXCEPTION,"exception occur");
		}
	}


}
